package com.zjw.zy.viewmodel

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.zjw.zy.base.BaseViewModel
import com.zjw.zy.bean.Tip
import com.zjw.zy.datasource.database.AppDataBase
import com.zjw.zy.ext.launchInIo
import com.zjw.zy.ext.requireService
import com.zjw.zy.utils.TimeUtils

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2022/11/2 10:44
 * @description：
 */
class TipsViewModel : BaseViewModel() {

    companion object {
        private const val TAG = "TipsViewModel"

        private const val REQUEST_TODAY = 0
        private const val REQUEST_PLAN = 1
        private const val REQUEST_DONE = 2
    }

    private var mCurrentRequestType = REQUEST_TODAY

    //计划
    private val mTips = MutableLiveData<List<Tip>>()
    fun getTips(): LiveData<List<Tip>> = mTips

    //tag
    private val mTags = MutableLiveData<List<String>>()
    fun getTags(): LiveData<List<String>> = mTags

    private val mTodayCount = MutableLiveData<Int>()
    fun getTodayCount(): LiveData<Int> = mTodayCount

    private val mPlanCount = MutableLiveData<Int>()
    fun getPlanCount(): LiveData<Int> = mPlanCount

    private val mDoneCount = MutableLiveData<Int>()
    fun getDoneCount(): LiveData<Int> = mDoneCount

    private val mTagCount = MutableLiveData<Int>()
    fun getTagCount(): LiveData<Int> = mTagCount

    fun requireTagTips(tag: String = "", isDone: Boolean = false) {
        launchInIo {
            if (tag.isNotEmpty()) {
                mTips.postValue(AppDataBase.getInstance().tipsDao().queryAllByTag(tag, isDone))
            } else {
                mTips.postValue(AppDataBase.getInstance().tipsDao().queryAllByDone(isDone))
            }
        }
    }

    fun requireDoneTips() {
        launchInIo {
            mCurrentRequestType = REQUEST_DONE
            mTips.postValue(AppDataBase.getInstance().tipsDao().queryAllByDone(true))
        }
    }

    fun requireTodayTips() {
        launchInIo {
            mCurrentRequestType = REQUEST_TODAY
            //包括过去的，没有完成的
            val all = AppDataBase.getInstance().tipsDao().queryAllByDone(false).filter { tip ->
                tip.isToday || tip.isPast
            }

            mTips.postValue(all)
        }
    }

    fun requirePlanTips() {
        launchInIo {
            mCurrentRequestType = REQUEST_PLAN
            val all = AppDataBase.getInstance().tipsDao().queryAllByDone(false)
                .filter { tip -> tip.isPlan }

            mTips.postValue(all)
        }
    }

    fun requireTags() {
        launchInIo {
            val all = AppDataBase.getInstance().tipsDao().queryAllTags()
            mTags.postValue(all)
        }
    }

    fun refreshLastRequest() {
        Log.d(TAG, "refreshLastRequest() called $mCurrentRequestType")
        when (mCurrentRequestType) {
            REQUEST_TODAY -> {
                requireTodayTips()
            }
            REQUEST_PLAN -> {
                requirePlanTips()
            }
            REQUEST_DONE -> {
                requireDoneTips()
            }
        }
    }

    fun finishTip(tip: Tip, call: Runnable) {
        launchInIo {
            tip.setDone(true)
            AppDataBase.getInstance().tipsDao().updateItem(tip)
            call.run()
        }
    }

    fun insert(tip: Tip) {
        launchInIo {
            AppDataBase.getInstance().tipsDao().insertItem(tip)
            requireService()?.addSchedulerForTips(tip)
        }
    }

    fun requireCount() {
        launchInIo {
            val all = AppDataBase.getInstance().tipsDao().queryAll()
            var today = 0
            var plan = 0
            var done = 0
            val tag = HashSet<String>()

            for (tip in all) {
                tag.add(tip.tag)
                if (tip.isToday || tip.isPast) {
                    today++
                }

                if (tip.isDone) {
                    done++
                }

                if (tip.isPlan) {
                    plan++
                }
            }

            mTodayCount.postValue(today)
            mPlanCount.postValue(plan)
            mDoneCount.postValue(done)
            mTagCount.postValue(tag.size)
        }
    }

    fun requireTipsByKeyword(keyword: String) {
        launchInIo {
            mTips.postValue(AppDataBase.getInstance().tipsDao().queryAll().filter { tip ->
                tip.tag.contains(keyword)
                        || tip.tip.contains(keyword)
                        || TimeUtils.getTimeBean(tip.time).stringOfYears.contains(keyword)
            })
        }
    }

    fun updateItem(tip: Tip) {
        launchInIo {
            AppDataBase.getInstance().tipsDao().updateItem(tip)

            refreshLastRequest()
        }
    }

}